package com.dididoctor.patient.Activity.Evaluate;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.MV.EduActivity;
import com.dididoctor.patient.R;
import com.dididoctor.patient.Utils.MyToast;
import com.dididoctor.patient.WYY.SessionHelper;
import com.netease.nim.uikit.session.activity.P2PMessageActivity;
import com.netease.nim.uikit.session.constant.Extras;

/**
 * Created by Administrator on 2016/4/14.
 */
public class EvaluateActivity extends EduActivity implements EvaluateView {

    private TextView mTvPoint;
    private EditText mEtEvaluate;

    private EvaluatePresenter presenter;
    private String hosId = "", remark = "", recId = "";

    private CheckBox[] checkBoxes;
    private CheckBox cbone, cbtwo, cbthree, cbfour, cbfive;
    private int index = 0;
    private String userid;
    private String doctorid;
    private String accId;
    private String token;
    private String name;
    private String sign;

    private boolean istrue = false;
    private boolean iszq = false;

    public static void start(Context context, String hosId, String recId) {
        Intent intent = new Intent();
        intent.putExtra("hosId", hosId);
        intent.putExtra("recId", recId);
        context.startActivity(intent);
    }

    @Override
    public void init() {
        initview();

        initdata();
    }

    private void initdata() {
        hosId = getIntent().getStringExtra("hosId");
        recId = getIntent().getStringExtra("recId");
        presenter = new EvaluatePresenter(this, this);
    }

    private void initview() {
        mTvPoint = (TextView) findViewById(R.id.tv_point);
        mEtEvaluate = (EditText) findViewById(R.id.et_evaluate);
        findViewById(R.id.btn_confirm).setOnClickListener(this);
        findViewById(R.id.reL_all).setOnClickListener(this);
        cbone = (CheckBox) findViewById(R.id.cb_one);
        cbtwo = (CheckBox) findViewById(R.id.cb_two);
        cbthree = (CheckBox) findViewById(R.id.cb_three);
        cbfour = (CheckBox) findViewById(R.id.cb_four);
        cbfive = (CheckBox) findViewById(R.id.cb_five);
        checkBoxes = new CheckBox[]{cbone, cbtwo, cbthree, cbfour, cbfive};

        cbone.setOnClickListener(this);
        cbtwo.setOnClickListener(this);
        cbthree.setOnClickListener(this);
        cbfour.setOnClickListener(this);
        cbfive.setOnClickListener(this);
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                if (!istrue) {
                    MyToast.DefaultmakeText(EvaluateActivity.this, "请选择评分", 0);
                    return;
                }

                if ("".equals(mEtEvaluate.getText().toString().trim())) {
                    MyToast.DefaultmakeText(EvaluateActivity.this, "请输入评价内容", 0);
                    return;
                }

                remark = mEtEvaluate.getText().toString();
                presenter.submitevaluate(hosId, (index + 1), remark, recId);
                break;
            case R.id.reL_all:
                break;
            case R.id.cb_one:
                index = 0;
                istrue = true;
                seleted(0);
                break;
            case R.id.cb_two:
                index = 1;
                istrue = true;
                seleted(1);
                break;
            case R.id.cb_three:
                index = 2;
                istrue = true;
                seleted(2);
                break;
            case R.id.cb_four:
                index = 3;
                istrue = true;
                seleted(3);
                break;
            case R.id.cb_five:
                index = 4;
                istrue = true;
                seleted(4);
                break;
        }
    }

    private void seleted(int i) {
        for (int j = 0; j <= 4; j++) {
            checkBoxes[j].setChecked(false);
        }
        for (int j = 0; j <= i; j++) {
            checkBoxes[j].setChecked(true);
        }
        if (i == 4) {
            mTvPoint.setText("满意");
        } else if (i == 3) {
            mTvPoint.setText("一般");
        } else {
            mTvPoint.setText("不满意");
        }

    }

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_evaluate);
        userid = getIntent().getStringExtra("userid");
        token = getIntent().getStringExtra("token");
        doctorid = getIntent().getStringExtra("doctorid");
        accId = getIntent().getStringExtra("accId");
        name = getIntent().getStringExtra("name");
        sign = getIntent().getStringExtra("sign");
        iszq = getIntent().getBooleanExtra("iszq", false);
    }

    @Override
    public void submitsucced() {
        if (iszq) {
            Intent intent = new Intent();
            intent.putExtra(Extras.EXTRA_ACCOUNT, accId);
            intent.putExtra(Extras.EXTRA_CUSTOMIZATION, SessionHelper.getP2pCustomization());
            intent.setClass(EvaluateActivity.this, P2PMessageActivity.class);
//          FLAG_ACTIVITY_SINGLE_TOP
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("start", "4");
            intent.putExtra("recid", recId);
            intent.putExtra("hosId", hosId);
            intent.putExtra("userid", Token.getId());
            intent.putExtra("token", Token.getToken());
            intent.putExtra("doctorid", doctorid);
            intent.putExtra("accId", accId);
            intent.putExtra("name", name);
            intent.putExtra("sign", sign);
            startActivity(intent);
        } else {
            showToastMessage("评价成功");
            setResult(RESULT_OK);
            finish();
        }

    }

    @Override
    public void submitfail() {
        showToastMessage("失败");
    }

}
